﻿using Helpers.Common;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using log4net.Repository.Hierarchy;
using System;
using System.IO;

namespace Helpers.Log
{
    /// <summary>
    /// A wrapper of the <typeparamref name="log4net.ILog"/> class. to add the functionality of ignore some repeated log events
    /// on a certain interval of time.
    /// </summary>
    public static class Logger
    {
        #region Static Properties

        /// <summary>
        /// Indicates if the log4net configuration was set.
        /// </summary>
        private static bool configured = false;

        #endregion

        #region Static Methods

        /// <summary>
        /// Gets the logger configured on the config file.
        /// </summary>
        /// <param name="loggerName">The logger name to obtain.</param>
        /// <returns></returns>
        public static ILog GetLogger(string loggerName)
        {
            if (Logger.configured == false)
            {
                string configFile = Helper.GetExecutingExePath() + AppDomain.CurrentDomain.FriendlyName + ".config";

                XmlConfigurator.ConfigureAndWatch(new FileInfo(configFile));

                Logger.configured = true;
            }

            return LogManager.GetLogger(loggerName);
        }
        public static void ConfigureByCode()
        {
            Logger.ConfigureByCode(Logger.GetDefaultPatterLayout(), Level.All);
        }
        public static void ConfigureByCode(PatternLayout patternLayout)
        {
            Logger.ConfigureByCode(patternLayout, Level.All);
        }
        public static void ConfigureByCode(Level level)
        {
            Logger.ConfigureByCode(Logger.GetDefaultPatterLayout(), level);
        }
        public static void ConfigureByCode(PatternLayout patternLayout, Level level)
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            patternLayout.ActivateOptions();

            TraceAppender tracer = new TraceAppender();
            tracer.Layout = patternLayout;
            tracer.ActivateOptions();
            hierarchy.Root.AddAppender(tracer);

            ColoredConsoleAppender coloredConsole = new ColoredConsoleAppender();
            coloredConsole.Layout = patternLayout;
            coloredConsole.ActivateOptions();
            hierarchy.Root.AddAppender(coloredConsole);

            hierarchy.Root.Level = level;
            hierarchy.Configured = true;


            Helpers.Log.Logger.ConfirmConfiguration();
        }
        public static void ConfirmConfiguration()
        {
            Logger.configured = true;
        }
        public static PatternLayout GetDefaultPatterLayout()
        {
            return new PatternLayout
            {
                ConversionPattern = "%d{yyyy-MM-dd hh:mm:ss.fff tt} %-5level %-23logger - %m%n",
            };
        }

        #endregion
    }
}